/**
 * ...
 * @author Yog
 */

package cookinmaddy.model;

import cookinmaddy.model.Ingredient;
import cookinmaddy.database.spod.DBIngredient;
import cookinmaddy.database.spod.DBIngredientManager;
import haxe.Serializer;
import php.Lib;

class IngredientManager 
{
	public function new() {}
	
	public function getList() : String
	{
		trace("BEGIN");
		try
		{
			var unitlist = DBIngredient.manager.allIngredients(); // _root.getSubUnits();
			return haxe.Serializer.run(unitlist);
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "";
	}
	
	public function update(value : Ingredient) : String
	{
		trace("BEGIN");
		try
		{
			var db = DBIngredient.manager.get(value.ID);
			db.value = value;
			db.update();
		}
		catch (e: String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "OK";
	}
	
	public function add(value : Ingredient) : String
	{
		trace("BEGIN");
		try
		{
			//trace("in add");
			var db : DBIngredient = new DBIngredient();
			db.value = value;
			db.insert();	
			return haxe.Serializer.run(db.value);
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "KO";
	}	
	
	public function delete(value : Ingredient) : String
	{
		trace("BEGIN");
		try
		{
			trace("get : " + value.ID);
			var db = DBIngredient.manager.get(value.ID);
			db.value = value;
			db.delete();
			return cast value.ID;
		}
		catch (e : String)
		{
			trace("ERROR: " + e);
		}
		trace("END");
		return "KO";
	}		
}